package com.concise.appapi.service;

import com.initialize.pub.dao.UserDAO;
import com.initialize.pub.generate.dao.UserMapper;
import com.initialize.pub.generate.pojo.User;
import com.initialize.pub.pojo.Result;
import com.initialize.pub.token.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @program: Initialization_Project
 * @description:
 * @author: Mr.Jane
 * @create: 2019-10-10 22:29
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class UserService {
    @Autowired
    UserDAO userDAO;

    @Autowired
    TokenService tokenService;

    @Autowired
    UserMapper userMapper;

    public Result login(Map<String, Object> stringObjectMap) {
        String login = stringObjectMap.get("login").toString();
        String psd = stringObjectMap.get("psd").toString();

        User user = userDAO.login(login, psd);
        Map<String, Object> userLoginInfoMap = new LinkedHashMap<>();

        if (!Objects.isNull(user)) {
            userLoginInfoMap.put("user", user);
            userLoginInfoMap.put("token", tokenService.getToken(user));

            return Result.success(userLoginInfoMap);
        } else {

            return Result.error("账号或密码错误");
        }
    }

    public Result UserInfo(int id) {

        return Result.error(userMapper.selectByPrimaryKey(id));
    }

    //    @Transactional(rollbackFor = Exception.class)
    public Result deleteUser(int id) throws Exception {

        userMapper.deleteByPrimaryKey(id);
        throw new Exception("异常了");
    }
}
